package com.hanson.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.exception.BusinessException;
import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.GroupGoodsBean;
import com.hanson.mapper.GroupGoodsBeanMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GroupGoodsService {
    @Autowired
    private GroupGoodsBeanMapper groupGoodsBeanMapper;

    public void add(GroupGoodsBean groupGoodsBean) {
        if (null == groupGoodsBean.getGoodsId()) {
            throw new BusinessException("商品ID不能为空");
        }
        List<GroupGoodsBean> groupGoodsBeanList = groupGoodsBeanMapper.selectByGoodsId(groupGoodsBean.getGoodsId());
        if (CollectionUtils.isNotEmpty(groupGoodsBeanList)) {
            throw new BusinessException("当前商品已在拼团中，不能重复添加");
        }
        if(groupGoodsBean.getGroupNum() < 2){
            throw new BusinessException("成团数量至少2个人");
        }
        groupGoodsBean.setCreateDate(new Date());
        groupGoodsBean.setStatus(0);
        groupGoodsBean.setIsShow(1);
        groupGoodsBean.setGoodsPrice(BusinessUtil.getDbAmount(groupGoodsBean.getWebGoodsPrice()));
        groupGoodsBean.setGroupPrice(BusinessUtil.getDbAmount(groupGoodsBean.getWebGoodsPrice()));
        groupGoodsBeanMapper.insert(groupGoodsBean);
    }

    public void modify(GroupGoodsBean groupGoodsBean) {
        if (null == groupGoodsBean.getGoodsId()) {
            throw new BusinessException("商品ID不能为空");
        }
        if(groupGoodsBean.getGroupNum() < 2){
            throw new BusinessException("成团数量至少2个人");
        }
        GroupGoodsBean returnBean = groupGoodsBeanMapper.selectByPrimaryKey(groupGoodsBean.getId());
        if (null != returnBean) {
            List<GroupGoodsBean> groupGoodsBeanList = groupGoodsBeanMapper.selectByGoodsId(groupGoodsBean.getGoodsId());
            if (CollectionUtils.isNotEmpty(groupGoodsBeanList) && !groupGoodsBeanList.get(0).getGoodsId().equals(returnBean.getGoodsId())) {
                throw new BusinessException("当前商品已在拼团中，不能重复添加");
            }
            groupGoodsBean.setUpdateDate(new Date());
            groupGoodsBean.setCreateDate(returnBean.getCreateDate());
            groupGoodsBean.setStatus(returnBean.getStatus());
            groupGoodsBean.setIsShow(returnBean.getIsShow());
            groupGoodsBean.setGoodsPrice(BusinessUtil.getDbAmount(groupGoodsBean.getWebGoodsPrice()));
            groupGoodsBean.setGroupPrice(BusinessUtil.getDbAmount(groupGoodsBean.getWebGroupPrice()));
            groupGoodsBean.setSales(returnBean.getSales());
            groupGoodsBean.setJoinNum(returnBean.getJoinNum());
            groupGoodsBean.setGroupedNum(returnBean.getGroupedNum());
            groupGoodsBean.setViewNum(returnBean.getViewNum());
            groupGoodsBeanMapper.updateByPrimaryKey(groupGoodsBean);
        }
    }

    public void mofiySimpleData(GroupGoodsBean groupGoodsBean) {
        groupGoodsBean.setUpdateDate(new Date());
        groupGoodsBeanMapper.updateByPrimaryKey(groupGoodsBean);
    }

    public GroupGoodsBean selectById(Integer id) {
        GroupGoodsBean groupGoodsBean = groupGoodsBeanMapper.selectByPrimaryKey(id);
        if (null != groupGoodsBean) {
            groupGoodsBean.setWebGoodsPrice(BusinessUtil.getWebAmount(groupGoodsBean.getGoodsPrice()));
            groupGoodsBean.setWebGroupPrice(BusinessUtil.getWebAmount(groupGoodsBean.getGroupPrice()));
        }
        return groupGoodsBean;
    }

    public void deleteById(Integer id) {
        groupGoodsBeanMapper.deleteByPrimaryKey(id);
    }

    public Page<GroupGoodsBean> listGroupGoods(Page<GroupGoodsBean> page, GroupGoodsBean groupGoodsBean) {
        List<GroupGoodsBean> list = groupGoodsBeanMapper.listGroupGoods(page, groupGoodsBean);
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(c -> {
                c.setWebGoodsPrice(BusinessUtil.getWebAmount(c.getGoodsPrice()));
                c.setWebGroupPrice(BusinessUtil.getWebAmount(c.getGroupPrice()));
            });
        }
        return page.setRecords(list);
    }

    public void updateIsShow(int isShow, int id) {
        groupGoodsBeanMapper.updateIsShow(isShow, id);
    }

    public List<GroupGoodsBean> getGroupGoods() {
        List<GroupGoodsBean> list = groupGoodsBeanMapper.selectCurrentTimeData();
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(c -> {
                c.setWebGoodsPrice(BusinessUtil.getWebAmount(c.getGoodsPrice()));
                c.setWebGroupPrice(BusinessUtil.getWebAmount(c.getGroupPrice()));
            });
        }
        return list;
    }
}
